import csv
import random
import string
import os
csvrows = []
csvrows.append(("id","ip_addr","rndnum","rndstr"))
for i in range(999999):
    ip_addr = str(random.randint(1,255)) + "." + str(random.randint(1,255)) + "." + str(random.randint(1,255)) + "." + str(random.randint(1,255))
    randnum = str(random.randint(0,9999))
    randstr = ''.join(random.choice(string.letters + string.digits) for i in xrange(10))
    csvrows.append((str(i),ip_addr,randnum,randstr))

cw = csv.writer(open("/tmp/csv.tmp", "wb"))
cw.writerows(csvrows)
csvrows = None
cw = None

cr = csv.DictReader(open("/tmp/csv.tmp", "rb"))
highestaddr = 0
curraddr = 0
for row in cr:
    if row:
        ip_addr_arr = []
        ip_addr = row["ip_addr"]
        ip_addr_arr = ip_addr.split(".")
        oct1 = bin(int(ip_addr_arr[0]))[2:].zfill(8)
        oct2 = bin(int(ip_addr_arr[1]))[2:].zfill(8)
        oct3 = bin(int(ip_addr_arr[2]))[2:].zfill(8)
        oct4 = bin(int(ip_addr_arr[3]))[2:].zfill(8)
        
        curraddr = int(oct1+oct2+oct3+oct4, 2)
        if highestaddr < curraddr:
            highestaddr = curraddr

cr = None
octhigh1 = 0
octhigh2 = 0
octhigh3 = 0
octhigh4 = 0
print "Highest IP Address generated (Decimal): " + str(highestaddr)
octhigh1 = highestaddr & 0xFF
octhigh2 = (highestaddr >> 8 ) & 0xFF
octhigh3 = (highestaddr >> 16 ) & 0xFF
octhigh4 = (highestaddr >> 24 ) & 0xFF
print "Highest IP Address generated (Dotted): " + str(octhigh4) + "." + str(octhigh3) + "." + str(octhigh2) + "." + str(octhigh1)
# os.remove("/tmp/csv.tmp")